/**************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Installer Framework.
**
** created by winter, 20181023
**
**************************************************************************/
#include "installerobjcommons.h"
//#include "WizardGui.h"

#include <packagemanagercore.h>
#include <scriptengine.h>
#include <packagemanagerpagefactory.h>
#include <productkeycheck.h>
#include <settings.h>

using namespace QInstaller;


// -- InstallerGui

InstallerObj::InstallerObj(PackageManagerCore *core)
	: PackageManagerObj(core, 0)
	//, m_gui(new WizardGui(this))
{
    /*ProductKeyCheck *checker = ProductKeyCheck::instance();
    foreach (const int id, checker->registeredPages()) {
        PackageManagerPage *page = PackageManagerPageFactory::instance().create(id, core);
        Q_ASSERT_X(page, Q_FUNC_INFO, qPrintable(QString::fromLatin1("Page with %1 couldn't be "
            "constructed.").arg(id)));
        setPage(id, page);
    }*/

    //setPage(0, PackageManagerCore::Introduction, (new IntroductionPageGui(new PackageManagerPageObj(this), m_gui))->objPage());
	setPage(0, PackageManagerCore::Introduction, new IntroductionPageObj(this));
	setPage(1, PackageManagerCore::PerformInstallation, new PerformInstallationPageObj(this));
/*    setPage(PackageManagerCore::TargetDirectory, new TargetDirectoryPage(core));
    setPage(PackageManagerCore::ComponentSelection, new ComponentSelectionPage(core));
    setPage(PackageManagerCore::LicenseCheck, new LicenseAgreementPage(core));
#ifdef Q_OS_WIN
    setPage(PackageManagerCore::StartMenuSelection, new StartMenuDirectoryPage(core));
#endif
    setPage(PackageManagerCore::ReadyForInstallation, new ReadyForInstallationPage(core));
    setPage(PackageManagerCore::InstallationFinished, new FinishedPage(core)); */

    foreach (const int id, pageIds()) {
		PackageManagerPageObj *wizardPage = page(id);
        packageManagerCore()->controlScriptEngine()->addToGlobalObject(wizardPage);
        packageManagerCore()->componentScriptEngine()->addToGlobalObject(wizardPage);
    }
    qDebug() << ">>> InstallerObj";
}

InstallerObj::~InstallerObj() {
	//if (m_gui)
	//	delete m_gui;
}


// -- MaintenanceGui

MaintenanceObj::MaintenanceObj(PackageManagerCore *core)
    : PackageManagerObj(core, 0)
	//, m_gui(new WizardGui(this))
{
    /*ProductKeyCheck *checker = ProductKeyCheck::instance();
    foreach (const int id, checker->registeredPages()) {
        PackageManagerPage *page = PackageManagerPageFactory::instance().create(id, core);
        Q_ASSERT_X(page, Q_FUNC_INFO, qPrintable(QString::fromLatin1("Page with %1 couldn't be "
            "constructed.").arg(id)));
        setPage(id, page);
    }*/

   /* IntroductionPage *intro = new IntroductionPage(core);
    connect(intro, &IntroductionPage::packageManagerCoreTypeChanged,
            this, &MaintenanceObj::updateRestartPage);

    if (!core->isOfflineOnly() || validRepositoriesAvailable()) {
        setPage(PackageManagerCore::Introduction, intro);
        setPage(PackageManagerCore::ComponentSelection, new ComponentSelectionPage(core));
        setPage(PackageManagerCore::LicenseCheck, new LicenseAgreementPage(core));
    } else {
        core->setUninstaller();
        core->setCompleteUninstallation(true);
    }

    setPage(PackageManagerCore::ReadyForInstallation, new ReadyForInstallationPage(core));
    setPage(PackageManagerCore::PerformInstallation, new PerformInstallationPage(core));
    setPage(PackageManagerCore::InstallationFinished, new FinishedPage(core));

    RestartPage *p = new RestartPage(core);
    connect(p, &RestartPage::restart, this, &PackageManagerGui::gotRestarted);
    setPage(PackageManagerCore::InstallationFinished + 1, p);*/

	setPage(0, PackageManagerCore::Introduction, new IntroductionPageObj(this));
	setPage(1, PackageManagerCore::PerformInstallation, new PerformInstallationPageObj(this));

    if (core->isUninstaller())
        wizardPageVisibilityChangeRequested(false, PackageManagerCore::InstallationFinished + 1);

    foreach (const int id, pageIds()) {
		PackageManagerPageObj *wizardPage = page(id);
        packageManagerCore()->controlScriptEngine()->addToGlobalObject(wizardPage);
        packageManagerCore()->componentScriptEngine()->addToGlobalObject(wizardPage);
    }
}

MaintenanceObj::~MaintenanceObj() {
	//if (m_gui)
	//	delete m_gui;
}

void MaintenanceObj::updateRestartPage()
{
    wizardPageVisibilityChangeRequested((packageManagerCore()->isUninstaller() ? false : true),
        PackageManagerCore::InstallationFinished + 1);
}

bool MaintenanceObj::validRepositoriesAvailable() const
{
    foreach (const Repository &repo, packageManagerCore()->settings().repositories()) {
        if (repo.isEnabled() && repo.isValid()) {
            return true;
        }
    }
    return false;
}
